import { LGXEnums } from '../../common/sysenum.js';
import globalconst from '../../common/globalConst.js'
var global = globalconst.GlobalConst;
import UtilFuncs from "../../common/tools/utilfuncs.js";
var funcs = UtilFuncs.UtilFuncs;

class TSrc2DstID {
    constructor() {
        this.srcID = 0;
        this.dstID = 0;
    }
}




class CopyMemBufManager {
    constructor() {
        this.m_tSrc2DstIDMap = new Array();
        this.m_tGraphBuf = new Array();
        this.m_tNodeBuf = new Array();
        this.m_tLinehBuf = new Array();
        this.m_tDevResBuf = new Array();
        this.m_tNewGraph = new Array();
        this.m_tNewNode = new Array();
        this.m_tNewLine = new Array();
        this.m_tNewDevRes = new Array();
        this.m_copyCntX = 0;
        this.m_copyCntY = 0;
    }

    CopyFromGraphLst(tGraphList, x, y) {
        this.m_copyCntX = x;
        this.m_copyCntY = y;
        let nGraph = tGraphList.length;
        for (let i = 0; i < nGraph; i++) {
            let pGraph = tGraphList[i];
            this.CopyFromGraph(pGraph, x, y);
        }
    }


    CopyFromResLst(tResList, x, y) {
        let nRes = tResList.lengt;
        for (let i = 0; i < nRes; i++) {
            let pRes = tResList[i];
            this.CopyFromRes(pRes, x, y);
        }
    }

    CopyFromGraph(pGraph, x, y) {
        if (!this.chkBufInList(this.m_tGraphBuf, pGraph.gid)) {
            var pGraphMem = pGraph.toInfo();
            this.m_tGraphBuf.push(pGraphMem);
        }
        return 0;
    }



    CopyFromRes(pRes, x, y) {
        let nType = funcs.mainObjType(pRes.id);
        if (nType == global.TopoNode) {
            if (!this.chkBufInList(this.m_tNodeBuf, pRes.id)) {
                let node = pRes.toInfo();
                this.m_tNodeBuf.push(node);
            }
        }
        else if (nType == global.ConnectLine) {
            if (!this.chkBufInList(this.m_tLinehBuf, pRes.id)) {
                let line = pRes.toInfo();
                this.m_tLinehBuf.push(lind);
            }
        }
        else {
            if (!this.chkBufInList(this.m_tDevResBuf, pRes.id)) {
                let devres = pRes.toInfo();
                this.m_tDevResBuf.push(devres);
            }
        }
    }

    initCopyPasteIDMap(tGraphList, tResList) {
        this.m_tSrc2DstIDMap.length = 0;
        let nGraph = tGraphList.length;
        for (let i = 0; i < nGraph; i++) {
            let pGraph = tGraphList[i];
            let pBuf = this.m_tGraphBuf[i];
            this.addToNewGraph(pGraph);
            this.AddIDMap(pBuf.gid, pGraph.m_gid, 1);
        }

        let nNodeIdx = 0;
        let nVirIdx = 0;
        let nOnOffIdx = 0;
        let nRes = tResList.length;
        for (let i = 0; i < nRes; i++) {
            let pRes = tResList[i];
            let nType = funcs.mainObjType(pRes.type);
            if (nType == global.TopoNode) {
                let pBuf = this.m_tNodeBuf[nNodeIdx];
                this.addToNewRes(pRes);
                this.AddIDMap(pBuf.id, pRes.id, 0);
                nNodeIdx++;
            }
            else if (nType == global.ConnectLine) {
                let pBuf = m_tLinehBuf[nVirIdx];
                this.addToNewRes(pRes);
                this.AddIDMap(pBuf.id, pRes.id, 0);
                this.nVirIdx++;
            }
            else {
                let pBuf = m_tDevResBuf[nOnOffIdx];
                this.addToNewRes(pRes);
                this.AddIDMap(pBuf.id, pRes.id, 0);
                this.nOnOffIdx++;
            }
        }
    }

    copySrcPropToDest(tGraphList, tResList, nx, ny) {
        this.initCopyPasteIDMap(tGraphList, tResList);

        let nGraph = tGraphList.length;
        for (let i = 0; i < nGraph; i++) {
            let pGraph = tGraphList[i];
            let pGraphBuf = this.m_tGraphBuf[i];
            this.CopyBufToGraphObj(pGraphBuf, pGraph, nx, ny);
            // if (pGraphBuf.m_nNewContainerID) {
            //     CAbsDrawing * pContainer = FindGraph(pGraphBuf.m_nNewContainerID);
            //     if (pContainer) {
            //         pContainer.addChild(pGraph);
            //     }
            // }
        }

        let nNodeIdx = 0;
        let nVirIdx = 0;
        let nOnOffIdx = 0;

        let nRes = tResList.length;
        let pRes = null;

        for (let i = 0; i < nRes; i++) {
            pRes = tResList[i];
            let nType = funcs.mainObjType(pRes.type);
            if (nType == global.TopoNode) {
                let pNode = pRes;
                let pBuf = this.m_tNodeBuf[nNodeIdx];
                pBuf.CopyTo(pNode, nx, ny);
                nNodeIdx++;
            }
            else if (nType == global.ConnectLine) {
                let pVir = pRes;
                let pBuf = m_tLinehBuf[nVirIdx];
                pBuf.CopyTo(pVir, nx, ny);
                nVirIdx++;
            }
            else {
                let pGraphRes = pRes;
                let pBuf = m_tDevResBuf[nOnOffIdx];
                pBuf.CopyTo(pGraphRes, nx, ny);
                nOnOffIdx++;
            }
        }

        return 0;
    }

    CopyBufToGraphObj(pGraphBuf, pGraph, nx, ny) {
        let offsetx = nx - this.m_copyCntX;
        let offsety = ny - this.m_copyCntY;
        pGraph.copyPropFromBuf(pGraphBuf, offsetx, offsety);
    }

    addToNewRes(pRes) {
        let nType = funcs.mainObjType(pRes.type);
        if (nType == global.TopoNode) {
            let nPos = this.chkBufInList(m_tNewNode, nID);
            if (!nPos) {
                m_tNewNode.push(pRes);
            }
        }
        else if (nType == global.ConnectLine) {
            let nPos = this.chkBufInList(m_tNewLine, nID);
            if (!nPos) {
                m_tNewLine.push(pRes);
            }
        }
        else if (funcs.isNoneLineConnDuctDev(pRes.m_nID)) {
            let nPos = this.chkBufInList(m_tNewDevRes, nID);
            if (!nPos) {
                m_tNewDevRes.push(pRes);
            }
        }

        return 1;
    }

    AddIDMap(srcID, dstID) {
        if (!this.getIDMapBySrcID(srcID)) {
            let pIDMap = new TSrc2DstID();
            pIDMap.srcID = srcID;
            pIDMap.dstID = dstID;
            this.m_tSrc2DstIDMap.push(pIDMap);
        }
    }

    getIDMapBySrcID(id) {
        let dest = null;
        this.m_tSrc2DstIDMap.forEach((e) => {
            if (e.srcID == id) {
                dest = e;
            }
        });
        return dest;
    }

    getIDMapByDestID(id) {
        let dest = null;
        this.m_tSrc2DstIDMap.forEach((e) => {
            if (e.dstID == id) {
                dest = e;
            }
        });
        return dest;
    }


    addToNewGraph(pGraph) {
        if (!this.chkObjInList(this.m_tNewGraph, pGraph.gid)) {
            this.m_tNewGraph.push(pGraph);
        }
    }

    chkBufInList(list, id) {
        list.forEach((e) => {
            if (e.id == id) {
                return true;
            }
        });
        return false;
    }

    chkObjInList(list, id) {
        list.forEach((e) => {
            if (e.gid == id) {
                return true;
            }
        });
        return false;
    }

    makeTopo(tNewGraphList) {
        this.makeGraphAttachLabel(tNewGraphList);
    }

    makeGraphAttachLabel(tNewGraphList) {
        let count = tNewGraphList.length;
        for (let i = 0; i < count; i++) {
            let pNewText = tNewGraphList[i];
            if (pNewText.m_graphType != LGXEnums.LGXGraphType.TextGraph_type){
                continue;
            }
            let tIDMap = this.getIDMapByDestID(pNewText.m_gid);
            if (tIDMap){
                let bufInfo = this.getBufInfo(tIDMap.srcID);
                if (bufInfo && bufInfo.parentGID != 0){
                    let graphIDMap = this.getIDMapBySrcID(bufInfo.parentGID);
                    if (graphIDMap){
                        let textAttachGraph = this.getNewGraphByGID(graphIDMap.dstID,tNewGraphList);
                        pNewText.setParentGraph(textAttachGraph);
                    }
                }
            }
        }
    }

    getNewGraphByGID(gid,tNewGraphList) {
        let destGraph = null;
        tNewGraphList.forEach(t => {
            if (t.m_gid == gid){
                destGraph = t;
                return ;
            }
        });
        return destGraph;
    }

    getBufInfo(gid) {
        let destBufInfo = null;
        this.m_tGraphBuf.forEach(t => {
            if (t.gid == gid){
                destBufInfo = t;
                return ;
            }
        });
        return destBufInfo;
    }

    clear() {
        this.m_tGraphBuf.length = 0;
        this.m_tNodeBuf.length = 0;
        this.m_tLinehBuf.length = 0;
        this.m_tDevResBuf.length = 0;
        this.m_tNewGraph.length = 0;
        this.m_tNewNode.length = 0;
        this.m_tNewLine.length = 0;
        this.m_tNewDevRes.length = 0;
        return 0;
    }
}
export default { CopyMemBufManager, TSrc2DstID }